package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.AssetsCodeRule;
import com.zyd.shiro.business.service.PakAssetsCodeRuleService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakAssetsCodeRule;
import com.zyd.shiro.persistence.mapper.PakAssetsCodeRuleMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakAssetsCodeRuleServiceImpl implements PakAssetsCodeRuleService {

    @Autowired
    private PakAssetsCodeRuleMapper pakAssetsCodeRuleMapper;

    @Override
    public AssetsCodeRule insert(AssetsCodeRule assetsCodeRule) {
        Assert.notNull(assetsCodeRule, "对象不可为空！");
        assetsCodeRule.getPakAssetsCodeRule().setUpdateTime(new Date());
        assetsCodeRule.getPakAssetsCodeRule().setCreateTime(new Date());
        pakAssetsCodeRuleMapper.insertSelective(assetsCodeRule.getPakAssetsCodeRule());
        return assetsCodeRule;
    }

    @Override
    public void insertList(List<AssetsCodeRule> assetsCodeRules) {
            for(AssetsCodeRule assetsCodeRule:assetsCodeRules){
               assetsCodeRule.getPakAssetsCodeRule().setUpdateTime(new Date());
               assetsCodeRule.getPakAssetsCodeRule().setCreateTime(new Date());
               pakAssetsCodeRuleMapper.insertSelective(assetsCodeRule.getPakAssetsCodeRule());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakAssetsCodeRuleMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(AssetsCodeRule assetsCodeRule) {
        Assert.notNull(assetsCodeRule, "对象不可为空！");
        assetsCodeRule.setUpdateTime(new Date());
        return pakAssetsCodeRuleMapper.updateByPrimaryKey(assetsCodeRule.getPakAssetsCodeRule()) > 0;
    }

    @Override
    public boolean updateSelective(AssetsCodeRule assetsCodeRule) {
        Assert.notNull(assetsCodeRule, "对象不可为空！");
        assetsCodeRule.setUpdateTime(new Date());
        return pakAssetsCodeRuleMapper.updateByPrimaryKeySelective(assetsCodeRule.getPakAssetsCodeRule()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public AssetsCodeRule getByPrimaryKey(Long primaryKey) {
        PakAssetsCodeRule pakAssetsCodeRule = pakAssetsCodeRuleMapper.selectByPrimaryKey(primaryKey);
        return null == pakAssetsCodeRule ? null : new AssetsCodeRule(pakAssetsCodeRule);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public AssetsCodeRule getOneByEntity(AssetsCodeRule test) {
        Assert.notNull(test, "对象不可为空！");
        PakAssetsCodeRule pakAssetsCodeRule = pakAssetsCodeRuleMapper.selectOne(test.getPakAssetsCodeRule());
        return null == pakAssetsCodeRule ? null : new AssetsCodeRule(pakAssetsCodeRule);
    }

    @Override
    public List<AssetsCodeRule> listAll() {
        List<PakAssetsCodeRule> pakAssetsCodeRules = pakAssetsCodeRuleMapper.selectAll();
        if (CollectionUtils.isEmpty(pakAssetsCodeRules)) {
            return null;
        }
        List<AssetsCodeRule> assetsCodeRules = new ArrayList<>();
        for (PakAssetsCodeRule pakAssetsCodeRule : pakAssetsCodeRules) {
            assetsCodeRules.add(new AssetsCodeRule(pakAssetsCodeRule));
        }
        return assetsCodeRules;
    }

    @Override
    public List<AssetsCodeRule> listByEntity(AssetsCodeRule entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<AssetsCodeRule> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakAssetsCodeRule> pakAssetsCodeRules = pakAssetsCodeRuleMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakAssetsCodeRules)) {
            return null;
        }
        List<AssetsCodeRule> assetsCodeRules = new ArrayList<>();
        for (PakAssetsCodeRule pakAssetsCodeRule : pakAssetsCodeRules) {
            assetsCodeRules.add(new AssetsCodeRule(pakAssetsCodeRule));
        }
        PageInfo bean = new PageInfo<PakAssetsCodeRule>(pakAssetsCodeRules);
        bean.setList(assetsCodeRules);
        return bean;
    }
@Override
    public void increase(PakAssetsCodeRule pakAssetsCodeRule) {
        pakAssetsCodeRuleMapper.insert(pakAssetsCodeRule);
    }




}
